/*
 * @Author: your name
 * @Date: 2021-05-23 15:38:37
 * @LastEditTime: 2021-05-24 22:00:41
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \filecontrol\src\main.rs
 */

 use std::fs::File;
 use std::io::prelude::*;
 
fn main() {

    // 分配2Mb大数组
    let mut flash_array = vec![0xffu8;2*1024*1024];
    let mut temp_array = vec![0xffu8;2*1024*1024/4];  // 临时数组
    
    
    let mut creatfile = File::create("lcd.bin").expect("create failed");
    println!("file create success:{:?}", creatfile);
    
    //文件读取操作
    // 读取多个文件合并到flash_array中
    let mut file = File::open("test.bin").unwrap();
    println!("文件打开成功:{:?}", file); 
    // 文件读入到数组
    file.read(&mut temp_array).unwrap();
    // 数组拷贝
    {
        let mut count:usize = 0; 
        let nbytes = temp_array.len(); 
        while count < nbytes {
            // 设定偏移地址
            flash_array[count+0x10] = temp_array[count];
            count += 1;
        }
    }
    //println!("test.bin has {}", flash_array[13]);
    
    // 写入文件
    {
        let mut count:usize = 0;
        let nbytes = flash_array.len();
        loop {
            
            
            creatfile.write(&flash_array[..nbytes]).unwrap();
            count += 1;
            if count < nbytes {break;}
        }
    }

    println!("Copy file ok!");
}
